package net.display;

import javax.servlet.http.HttpServletRequest;

import net.bo.KFonction;
import net.bo.KRubrique;
import net.ko.displays.KObjectDisplay;
import net.ko.http.objects.KRequest;
import net.ko.http.views.KPageList;
import net.ko.http.views.KobjectHttpAbstractView;
import net.ko.kobject.KListObject;
import net.ko.kobject.KObject;
import net.ko.utils.KString;

public class DisplayRubrique extends KObjectDisplay {

	@Override
	public void beforeLoading(Class<? extends KObject> clazz, KPageList list,
			HttpServletRequest request) {
		// TODO Auto-generated method stub

		if (!KString.isNull(KRequest.GETPOST("idPlan", request))) {
			String idPlan = KRequest.GETPOST("idPlan", request);
			list.addWhere("idPlan=" + idPlan);
			request.getSession().setAttribute("idPlan", idPlan);

		} else if (!KString.isNull(KRequest.GETPOST("idRubrique", request))) {
			String idRubrique = KRequest.GETPOST("idRubrique", request);
			list.addWhere("id=" + idRubrique);
		} else {
			Object idPlan = request.getSession().getAttribute("idPlan");
			list.addWhere("idPlan=" + idPlan);
		}
		super.beforeLoading(clazz, list, request);
	}

	@Override
	public void afterLoading(KObject ko, KobjectHttpAbstractView view,
			HttpServletRequest request) {
		// TODO Auto-generated method stub
		// KPlan plan = (KPlan) request.getSession().getAttribute("plan");
		// KRubrique rubrique = (KRubrique) ko;
		// rubrique.setIdPlan(Integer.valueOf(plan.getId() + ""));
		super.afterLoading(ko, view, request);
	}

	@Override
	public String getZone(String zoneName, KPageList list, HttpServletRequest request) {
		String result = super.getZone(zoneName, list, request);
		if ("_btReturn".equals(zoneName)) {
			if (!KString.isNull(KRequest.GETPOST("showBtReturn", request))) {

				result = "<div> <input type='button' class='btn' id='btReturnRubrique' value='Retour'/> </div>";

			} else {
				result = "<div> <input style='display:none;' type='button' class='btn' id='btReturnRubrique' value='Retour'/> </div>";
			}
		} else if ("_addBtn".equals(zoneName)) {
			if (!KString.isNull(KRequest.GETPOST("showBtReturn", request))) {
				result = "";
			}
		}

		return result;
	}

	@Override
	public String showInList(KObject ko, String memberName, HttpServletRequest request) {
		String res = super.showInList(ko, memberName, request);
		KRubrique rubrique = (KRubrique) ko;
		if ("libelle".equals(memberName)) {
			res = "<a id='" + rubrique.getId() + "' class='showFonction'>" + rubrique.getLibelle() + "</a>";
		}
		if ("nbfct".equals(memberName)) {
			KListObject<KFonction> listFonction = rubrique.getFonctions();
			res = String.valueOf(listFonction.count());
		}
		return res;
	}
}
